<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        ul,li{
            list-style: none;
        }
        a{
            text-decoration: none;
            display: block;
            font-size: 26px;
            line-height: 26px;
            padding: 4px 20px;
            border: 1px solid #000;
            color: #000;
        }
        ul{
            width: 500px;
            overflow: hidden;
            margin: 100px auto 0;
        }
        li{
            float: left;
        }
        section{
            position: relative;
            width: 500px;
            margin: 0 auto;
        }
        p{
            width: 500px;
            height: 200px;
            font-size: 20px;
            line-height: 200px;
            text-align: center;
            border: 1px solid #000;
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }
        .active{
            background: red;
            color: white;
        }
    </style>
</head>
<body>
    <ul>
        <li><a href="#" class="active">体育</a></li>
        <li><a href="#">军事</a></li>
        <li><a href="#">娱乐</a></li>
        <li><a href="#">经济</a></li>
    </ul>
    <section>
        <p style="display: block;">体育</p>
        <p>军事</p>
        <p>娱乐</p>
        <p>经济</p>
    </section>

    <script>
        var tabs = document.getElementsByTagName('a');
        var article = document.getElementsByTagName('p');

        // 后期es6的let可以不用index储存

        for(var i=0;i<tabs.length;i++){
            // 设置并储存点击的tab的索引值index
            tabs[i].index = i;
            tabs[i].onclick = function(){
                for(var j=0;j<tabs.length;j++){
                    // 排他思想
                    tabs[j].className = '';
                    article[j].style.display = 'none';
                }
                // this指向的tab添加类名
                this.className = 'active';
                // 用储存的索引值把对应的article显示出来
                article[this.index].style.display = 'block';
            }
        }
    </script>
</body>
</html>